﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Linq;
using System.Windows.Forms;
using DevExpress.XtraEditors;

namespace PRO_DOCS.DocumentManagementSystem.ADMIN.WIN.Configurations.Documents.DocumentTypes
{
    public partial class DocumentTypes : BLL.WIN.Templates.DevxConfigurationUserControlTemplate
    {
        DAL.Database.ARCHIVE_DOCUMENT_TYPE _cURRENT_ARCHIVE_DOCUMENT_TYPE;
        DAL.Database.ARCHIVE_DOCUMENT_TYPE CURRENT_ARCHIVE_DOCUMENT_TYPE
        {
            get { return _cURRENT_ARCHIVE_DOCUMENT_TYPE; }
            set { _cURRENT_ARCHIVE_DOCUMENT_TYPE = value; }
        }

        Configurations.Documents.DocumentTypes.DocumentTypesIndecies DocumentTypesIndecies
        {
            get;
            set;
        }

        public DocumentTypes()
        {
            InitializeComponent();

            panel1.Controls.Add(new Configurations.Documents.DocumentTypes.DocumentTypesIndecies());
            panel1.Controls[0].Dock = DockStyle.Fill;

            DocumentTypesIndecies = (Configurations.Documents.DocumentTypes.DocumentTypesIndecies)panel1.Controls[0];

            btnAddNew.ItemClick += new DevExpress.XtraBars.ItemClickEventHandler(btnAddNew_ItemClick);
            btnSaveUpdate.ItemClick += new DevExpress.XtraBars.ItemClickEventHandler(btnSaveUpdate_ItemClick);
            btnDeleteItem.ItemClick += new DevExpress.XtraBars.ItemClickEventHandler(btnDeleteItem_ItemClick);
            btnRefresh.ItemClick += new DevExpress.XtraBars.ItemClickEventHandler(btnRefresh_ItemClick);
            RELOAD_ALL_DOCUMENT_TYPES();
        }

        void btnRefresh_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            RELOAD_ALL_DOCUMENT_TYPES();
        }

        void btnDeleteItem_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            if (CURRENT_ARCHIVE_DOCUMENT_TYPE.ARCHIVE_DOCUMENT_TYPE_ID > 0)
            {
                try
                {
                    PRO_DOCSDatabaseDataContext.proc_ARCHIVE_DOCUMENT_TYPESDelete
                        (CURRENT_ARCHIVE_DOCUMENT_TYPE.ARCHIVE_DOCUMENT_TYPE_ID);
                }
                catch
                {
                    XtraMessageBox.Show("The document type cannot be deleted as it is already used in another transaction(s)", "Action denied", MessageBoxButtons.OK, MessageBoxIcon.Information);
                }
            }
        }

        void btnSaveUpdate_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            if (!string.IsNullOrEmpty(txtDocumentTypeName.Text))
            {
                Int64? aRCHIVE_DOCUMENT_TYPE_ID = 0;
                if (CURRENT_ARCHIVE_DOCUMENT_TYPE.ARCHIVE_DOCUMENT_TYPE_ID.Equals(0))
                {
                    PRO_DOCSDatabaseDataContext.proc_ARCHIVE_DOCUMENT_TYPESInsert
                        (
                        ref aRCHIVE_DOCUMENT_TYPE_ID,
                        txtDocumentTypeName.Text,
                        DateTime.Now,
                        BLL.WIN.Authentication.LOGGED_IN_EMPLOYEE_DETAILS.LOGIN_USER.LOGIN_USER_ID,
                        null,
                        null,
                        txtNotes.Text,
                        !chkIsValid.Checked);

                    DocumentTypesIndecies.LOAD_ALL_DOCUMENT_TYPE_DYNAMIC_INDECIES(aRCHIVE_DOCUMENT_TYPE_ID.Value);
                    RELOAD_ALL_DOCUMENT_TYPES();
                }
                else
                {
                    aRCHIVE_DOCUMENT_TYPE_ID = CURRENT_ARCHIVE_DOCUMENT_TYPE.ARCHIVE_DOCUMENT_TYPE_ID;
                    PRO_DOCSDatabaseDataContext.proc_ARCHIVE_DOCUMENT_TYPESUpdate
                        (aRCHIVE_DOCUMENT_TYPE_ID,
                        txtDocumentTypeName.Text,
                        CURRENT_ARCHIVE_DOCUMENT_TYPE.ARCHIVE_DOCUMENT_TYPE_CREATION_DATE.Value,
                        CURRENT_ARCHIVE_DOCUMENT_TYPE.ARCHIVE_DOCUMENT_TYPE_CREATED_BY_USER_ID.Value,
                        DateTime.Now,
                        BLL.WIN.Authentication.LOGGED_IN_EMPLOYEE_DETAILS.LOGIN_USER.LOGIN_USER_ID,
                        txtNotes.Text,
                        !chkIsValid.Checked);

                    gridView1.SetRowCellValue(gridView1.FocusedRowHandle, gcDocumentTypeName, txtDocumentTypeName.Text);
                    gridView1.SetRowCellValue(gridView1.FocusedRowHandle, gcNotes, txtNotes.Text);
                    gridView1.SetRowCellValue(gridView1.FocusedRowHandle, gcIsValid, !chkIsValid.Checked);

                }
            }
            else
            {
                dxErrorProvider1.SetError(txtDocumentTypeName, BLL.WIN.Properties.ErrorProviderMessages.Default.InputLeftBlank.Replace("{name}", "Document type name"));
            }
        }

        private void RELOAD_ALL_DOCUMENT_TYPES()
        {
            panel1.Enabled = false;
            CURRENT_ARCHIVE_DOCUMENT_TYPE = new DAL.Database.ARCHIVE_DOCUMENT_TYPE();
            txtDocumentTypeName.Text = string.Empty;
            txtNotes.Text = string.Empty;
            chkIsValid.Checked = true;
            gridControl1.DataSource = PRO_DOCSDatabaseDataContext.ARCHIVE_DOCUMENT_TYPEs.ToList();

        }

        void btnAddNew_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            //RELOAD_ALL_DOCUMENT_TYPES();

            panel1.Enabled = false;
            CURRENT_ARCHIVE_DOCUMENT_TYPE = new DAL.Database.ARCHIVE_DOCUMENT_TYPE();
            txtDocumentTypeName.Text = string.Empty;
            txtNotes.Text = string.Empty;
            chkIsValid.Checked = true;

        }

        private void gridView1_FocusedRowChanged(object sender, DevExpress.XtraGrid.Views.Base.FocusedRowChangedEventArgs e)
        {
            if (e.FocusedRowHandle > -1)
            {
                panel1.Enabled = true;
                CURRENT_ARCHIVE_DOCUMENT_TYPE = gridView1.GetFocusedRow() as DAL.Database.ARCHIVE_DOCUMENT_TYPE;
                DocumentTypesIndecies.LOAD_ALL_DOCUMENT_TYPE_DYNAMIC_INDECIES(CURRENT_ARCHIVE_DOCUMENT_TYPE.ARCHIVE_DOCUMENT_TYPE_ID);
                txtDocumentTypeName.Text = CURRENT_ARCHIVE_DOCUMENT_TYPE.ARCHIVE_DOCUMENT_TYPE_NAME;
                txtNotes.Text = CURRENT_ARCHIVE_DOCUMENT_TYPE.ARCHIVE_DOCUMENT_TYPE_NOTES;
                chkIsValid.Checked = !CURRENT_ARCHIVE_DOCUMENT_TYPE.IS_DELETED;
            }
        }

        private void DocumentTypes_Load(object sender, EventArgs e)
        {

        }
    }
}
